1、工程创建
创建一个基本的Spring Boot工程,添加Web依赖,MyBatis依赖以及MySQL驱动依赖,如下图:

创建成功后,添加Druid依赖,并且锁定MySQL驱动版本,完整的依赖如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.27</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
2、多数据源配置
这里首先在application.properties中配置数据库基本信息,然后提供两个DataSource即可,直接上代码。
application.properties中的配置:
spring.datasource.one.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8 spring.datasource.one.username=root spring.datasource.one.password=root spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.two.url=jdbc:mysql:///test02?useUnicode=true&characterEncoding=utf-8 spring.datasource.two.username=root spring.datasource.two.password=root spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
|
再提供两个DataSource配置,如下:
@Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.one") DataSource dsOne() { return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.two") DataSource dsTwo() { return DruidDataSourceBuilder.create().build(); } }
|
3、MyBatis配置
MyBatis的配置,不同于JdbcTemplate,MyBatis的配置要稍微麻烦一些,因为要提供两个Bean,因此这里两个数据源我将在两个类中分开来配置,首先来看第一个数据源的配置:
@Configuration @MapperScan(basePackages = "org.sang.mybatis.mapper1",sqlSessionFactoryRef = "sqlSessionFactory1",sqlSessionTemplateRef = "sqlSessionTemplate1") public class MyBatisConfigOne { @Resource(name = "dsOne") DataSource dsOne; @Bean SqlSessionFactory sqlSessionFactory1() { SqlSessionFactory sessionFactory = null; try { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dsOne); sessionFactory = bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sessionFactory; } @Bean SqlSessionTemplate sqlSessionTemplate1() { return new SqlSessionTemplate(sqlSessionFactory1()); } }
|
MyBatisConfigOne类,首先指明该类是一个配置类,配置类中要扫描的包是org.sang.mybatis.mapper1,即该包下的Mapper接口将操作dsOne中的数据,对应的SqlSessionFactory和SqlSessionTemplate分别是sqlSessionFactory1和sqlSessionTemplate1,在MyBatisConfigOne内部,分别提供SqlSessionFactory和SqlSessionTemplate即可,SqlSessionFactory根据dsOne创建,然后再根据创建好的SqlSessionFactory创建一个SqlSessionTemplate。
这里配置完成后,依据这个配置,再来配置第二个数据源即可:
@Configuration @MapperScan(basePackages = "org.sang.mybatis.mapper2",sqlSessionFactoryRef = "sqlSessionFactory2",sqlSessionTemplateRef = "sqlSessionTemplate2") public class MyBatisConfigTwo { @Resource(name = "dsTwo") DataSource dsTwo; @Bean SqlSessionFactory sqlSessionFactory2() { SqlSessionFactory sessionFactory = null; try { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dsTwo); sessionFactory = bean.getObject(); } catch (Exception e) { e.printStackTrace(); } return sessionFactory; } @Bean SqlSessionTemplate sqlSessionTemplate2() { return new SqlSessionTemplate(sqlSessionFactory2()); } }
|
4、mapper创建
mapper1:
public interface UserMapperOne { List<User> getAllUser(); }
|
对应的XML文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.sang.mybatis.mapper1.UserMapperOne"> <select id="getAllUser" resultType="org.sang.mybatis.model.User"> select * from t_user; </select> </mapper>
|
mapper2:
public interface UserMapper { List<User> getAllUser(); }
|
对应的XML文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.sang.mybatis.mapper2.UserMapper"> <select id="getAllUser" resultType="org.sang.mybatis.model.User"> select * from t_user; </select> </mapper>
|
最后单元测试,运行测试类:
@Autowired UserMapperOne UserMapperOne;
@Autowired UserMapper userMapper; @Test public void contextLoads() { List<User> allUser = UserMapperOne.getAllUser(); System.out.println(allUser); List<User> allUser2 = userMapper.getAllUser(); System.out.println(allUser2); }
|